package com.example.soa_exp2.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.soa_exp2.Domain.Admin;
import com.example.soa_exp2.Mapper.AdminMapper;
import com.example.soa_exp2.Service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private Admin admin;

    @Resource
    private AdminMapper adminMapper;

    @Override
    public List<Admin> getAdminList(){
        return adminMapper.selectList(null);
    }

    @Override
    public List<Admin> getAdminByAccount(String account){
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("account", account);
        return adminMapper.selectList(wrapper);
    }

    @Override
    public int addAdmin(Admin admin) {
        List<Admin> admins = getAdminList();
        for (Admin admin_a: admins) {
            if(Objects.equals(admin_a.getAccount(), admin.getAccount())){
                return 0;
            }
        }
        return adminMapper.insert(admin);
    }

    @Override
    public int updateAdmin(Admin admin){
        return adminMapper.updateById(admin);
    }

    @Override
    public int deleteAdmin(String account){
        return adminMapper.deleteById(account);
    }
}
